package multithread.thread.yeadjoin;

/**
 * Created by xi on 2019/1/15.
 */
public class Test extends  Thread{

    private Object object = new Object();

    public Test(String name) {
        super(name);
    }

    @Override
    public void run() {

    }

    private static void testJoin (){
        synchronized (Test.class){
            System.out.println(Thread.currentThread().getName() + "线程执行了 .. ");
            try {
                thread2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static Thread thread2;

    /**
     * join的线程不能使用A的对象锁，不然就会死锁；
     *
     * @param args
     */
    public static void main(String[] args) {
         thread2 = new Thread(){
            @Override public void run() {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"线程2开始执行");
                synchronized (Test.class){
                    System.out.println(Thread.currentThread().getName()+"thread2232323");
                }
            }
        };

        Thread thread1 = new Thread(){
            @Override public void run() {
                testJoin();
            }
        };
        thread1.start();
        thread2.start();
    }

    
}
